<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\facade\Request;
class User extends Controller
{
    protected $middleware = ['VerifyLogin'];

    /*
     * 用户个人资料获取
     * */
    public function info()
    {
        $UserLogic = \think\facade\App::model('User', 'logic');
        $result = $UserLogic->getInfo();

        if($result)
        {
            unset($result['book_coin']);
            unset($result['vip_exp_time']);
            return json(['code'=>0,'msg'=>'success','data'=>$result]);
        }

        return json(['code'=>103,'msg'=>'数据为空','data'=>[]]);
    }

    /*
     * 阅读记录
     * */
    public function reads()
    {
        $params = Request::get('', '', 'strip_tags,trim');

        $ReadHistoryLogic = \think\facade\App::model('ReadHistory', 'logic');
        $result = $ReadHistoryLogic->getList($params);

        if($result)
        {
            return json(['code'=>0,'msg'=>'success','data'=>$result]);
        }

        return json(['code'=>102,'msg'=>'数据为空','data'=>[]]);
    }

    /*
     * 阅读记录删除
     * */
    public function reads_del()
    {
        $params = Request::post('', '', 'strip_tags,trim');

        $verify_result = $this->validate($params, 'Read.del');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $ReadHistoryLogic = \think\facade\App::model('ReadHistory', 'logic');
        $result = $ReadHistoryLogic->readDel($params);

        if($result)
        {
            return json(['code'=>0,'msg'=>'success','data'=>$result]);
        }

        return json(['code'=>102,'msg'=>'数据为空','data'=>[]]);
    }

    /*
     * 消费记录
     * */
    public function sale()
    {
        $params = Request::get('', '', 'strip_tags,trim');
        $SaleLogic = \think\facade\App::model('SaleHistory', 'logic');
        $result = $SaleLogic->getList($params);

        if($result)
        {
            return json(['code'=>0,'msg'=>'success','data'=>$result]);
        }

        return json(['code'=>102,'msg'=>'数据为空','data'=>[]]);
    }

    /*
     * 用户签到添加
     * */
    public function sign_add()
    {
        $params = Request::post('', '', 'strip_tags,trim');

        $data['create_date']    = date('Y-m-d');
        $data['uid']            = request()->userInfo['id'];

        $SignLogic = \think\facade\App::model('Sign', 'logic');
        $result = $SignLogic->signInGoldCoin($data);

        if($result)
        {
            return json(['code'=>0,'msg'=>'success','data'=>$result]);
        }

        return json(['code'=>102,'msg'=>'数据为空','data'=>[]]);
    }

    /*
     * 用户签到奖励获取
     * */
    public function sign_rules()
    {
        $sign_rules = Db::table('quick_sign_config')->where('manage_id',request()->agentId)
            ->field('sign_data')->findOrEmpty();

        if(empty($sign_rules))
        {
            $sign_rules = Db::table('quick_sign_config')->where('manage_id',0)
                ->field('sign_data')->findOrEmpty();
        }

        $sign_rules = json_decode($sign_rules['sign_data'],true);

        $record = Db::table('quick_user_book_points')->where('uid',request()->userInfo['id'])
                ->order('id DESC')->field('continuous_days')->findOrEmpty();

        $sign_days = $record ? $record['continuous_days'] : 0;

        $data = [];
        foreach ($sign_rules as $k=>$v)
        {
            $data[] = [
                'virtual_currency' => $v['point'],
                'day' => $k,
                'is_lock' => $k <= $sign_days ? 1 : 0,
            ];
        }

        $date = date('Y-m-d');
        $record = Db::table('quick_user_book_points')->where('date',$date)
            ->where('uid',request()->userInfo['id'])->field('id')->findOrEmpty();

        $sign_today = $record ? 1 : 0;

        return json(['code'=>0,'msg'=>'success','data'=>['signin_days'=>$sign_days,'sign_today'=>$sign_today,'list'=>$data]]);
    }

    /*
     * 客服信息获取
     * */
    public function customer()
    {
        $CustomerLogic = \think\facade\App::model('Customer', 'logic');
        $record = $CustomerLogic->getInfo();

        return json(['code'=>0,'msg'=>'success','data'=>$record]);
    }

}

